/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package simplepushserver.filesend;

import java.io.*;


/**
 * @author Filip
 */
class ByteBlockReciver implements Runnable{
    private InputStream is;
    private OutputStream out;
    private int BUFFER;

    public ByteBlockReciver(InputStream is, OutputStream out, int BUFFER) {
        this.is = is;
        this.out = out;
        this.BUFFER=BUFFER;
    }

     public void run(){
        try {
            byte[] data = new byte[BUFFER];
            int count = 0;
            while ((count = is.read(data, 0, BUFFER)) != -1) {
                out.write(data, 0, count);
            }
            /*do{
                 while( is.available() < BUFFER && is.){
                int av = is.available();
                try {
                System.out.println("Wait For Avalibilty!");
                Thread.sleep(500);
                } catch (InterruptedException ex) {
                ex.printStackTrace();
                }
                }
                count = is.read(data, 0, BUFFER);
                System.out.println("Reading "+data.toString()+" byte! "+is.available());
                out.write(data, 0, count);
            }while (count!=-1);*/
        } catch (IOException ex) {
            throw  new  RuntimeException("Data Reading Failed\n",ex);
        }
     }
}
class ByteBlockSender implements Runnable{
    private InputStream in;
    private OutputStream out;
    private final int BUFFER;

    public ByteBlockSender(InputStream fileIn, OutputStream out, int BUFFER) {
        this.in = fileIn;
        this.out = out;
        this.BUFFER = BUFFER;
    }

     public void run(){
        try {
            byte[] data = new byte[BUFFER];
            int count = 0;
            while ((count = in.read(data, 0, BUFFER)) != -1) {
                out.write(data, 0, count);
            }
        } catch (IOException ex) {
            throw  new  RuntimeException("Data Sending Failed\n",ex);
        }
     }
}
